package com.armrest.screen.provider

import android.media.MediaPlayer
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.armrest.screen.*
import com.armrest.screen.widget.RecyclerViewPager
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder

class MusicModelProvider(var layout : Int = R.layout.car_ui_music_layout, var itemViewTypes :
              Int = ItemViewType.ITEM_TYPE_GRAVITY_MODEL
) : BaseItemProvider<HomeMenuModel>() {
    override val itemViewType: Int
        get() = itemViewTypes
    override val layoutId: Int
        get() = layout
    var  playBoolean : Boolean = false
    private var mediaPlayer : MediaPlayer? = null
    val adapter by lazy {
         MusicAdapter().apply {
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
             addData(HomeMenuModel(imageId= R.mipmap.imaga_icon))
             addData(HomeMenuModel(imageId= R.mipmap.card_middle))
         }
    }
    override fun convert(helper: BaseViewHolder, item: HomeMenuModel) {
      val list=helper.getViewOrNull<RecyclerView>(R.id.imageList)
      val play=helper.getViewOrNull<ImageView>(R.id.play)
        list?.let {
            it.layoutManager= LinearLayoutManager(it.context, LinearLayoutManager.VERTICAL, true)
            it.adapter=adapter
            val span= PagerSnapHelper()
             span.attachToRecyclerView(it)
             span.onFling(20,20)
            span.calculateScrollDistance(20,30)
        }
        play?.let { play_it->
            play_it.setOnClickListener {
                it.setMediaPlayer()
                play_it.setImageResource(if(playBoolean) R.mipmap.icon_m_play else R.mipmap.icon_pause_puse)
                 if(mediaPlayer == null){
                     mediaPlayer = MediaPlayer.create(this.context, R.raw.dijiadj)
                    mediaPlayer?.start()
                 }else{
                     if(mediaPlayer?.isPlaying == true){
                         mediaPlayer?.pause()
                     }else{
                         mediaPlayer?.start()
                     }
                 }
                playBoolean=!playBoolean
            }
        }
    }
    private val listener =object : RecyclerView.OnScrollListener(){
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
        }
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            val childCount: Int = recyclerView.childCount
            val width: Int = recyclerView.getChildAt(0).width
            val padding: Int = (recyclerView.width - width) / 2
            for (j in 0 until childCount) {
                val v = recyclerView.getChildAt(j)
                var rate = 0f
                if (v.left <= padding) {
                    rate = if (v.left >= padding - v.width) {
                      (padding - v.left) * 1f / v.width
                    } else { 1f }
                    v.scaleY = 1 - rate * 0.1f
                } else {
                    if (v.left <= recyclerView.width - padding) {
                        rate = (recyclerView.width - padding - v.left) * 1f / v.width
                    }
                    v.scaleY = 0.9f + rate * 0.1f
                }
            }
        }
    }

}